package com.ror.domain;

import java.util.ArrayList;

public class User {
    private static final ArrayList<User> users = new ArrayList<>();

    private String uname;
    private String pwd;

    static {
        //模拟添加用户
        addUser("admin","admin");
        addUser("api", "sdnc");
        addUser("xrb", "kinoko");
        addUser("12dora", "123");
    }

    private User() {
    }

    private User(String uname, String pwd) {
        this.uname = uname;
        this.pwd = pwd;
    }

    /**
     * 获取
     *
     * @return uname
     */
    public String getUname() {
        return uname;
    }

    /**
     * 设置
     *
     * @param uname
     */
    public void setUname(String uname) {
        this.uname = uname;
    }

    /**
     * 获取
     *
     * @return pwd
     */
    public String getPwd() {
        return pwd;
    }

    /**
     * 设置
     *
     * @param pwd
     */
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String toString() {
        return "com.ror.domain.User{uname = " + uname + ", pwd = " + pwd + "}";
    }

    public static void addUser(String uname, String pwd) {
        if (uname.isEmpty() || pwd.isEmpty()) return;
        users.add(new User(uname, pwd));
    }

    public static User getUser(String uname) {
        for (User user : users) {
            if (user.uname.equals(uname)) return user;
        }
        return null;
    }

    public static User removeUser(String uname) {
        User user = getUser(uname);
        if (user == null) return null;
        users.remove(user);
        return user;
    }

    public static User setUserName(String oldUname, String newUname) {
        User user = getUser(oldUname);
        if (user == null) return null;
        user.uname = newUname;
        return user;
    }

    public static User setUserPwd(String uname, String pwd) {
        User user = getUser(uname);
        if (user == null) return null;
        user.pwd = pwd;
        return user;
    }

    public static boolean checkPwd(String uname, String pwd) {
        User user = getUser(uname);
        return user != null && user.pwd.equals(pwd);
    }
}
